HAL WWDG Typical Use Cases for Migration

This section provides information about the HAL WWDG typical use case for migration .

HAL WWDG early wakeup interrupt

The table below outlines the typical scenarios for HAL1 and HAL2 regarding the WWDG peripheral:

In this scenario the user start the wwdg driver and receive a Callbacks (early wakeup Callbacks) that indicates a reset is going to occur, so the user can take the necessary actions before the reset.

HAL Version

Description

HAL1

In HAL1, the initialization of the WWDG peripheral was handled by a single init function. This function was responsible for both initializing and configuring its parameters, such as the Prescaler, Window, and Counter.

Steps:

  1. Uses HAL_WWDG_Init() to start the WWDG peripheral.

  2. Uses HAL_WWDG_RegisterCallback() to register the early wakeup interrupt

  3. Uses HAL_WWDG_Refresh() to refresh the WWDG peripheral.

  4. if wwdg interrupt raises, driver calls User_Callback_function .

HAL2

In HAL2, the initialization process for the WWDG driver was refined by splitting the original init API into two distinct functions: init and start.

Steps:

  1. Uses HAL_WWDG_Init() to initialize the WWDG peripheral.

  2. Uses HAL_WWDG_RegisterEarlyWakeupCallback() to register the early wakeup interrupt

  3. Uses HAL_WWDG_Start() to start the WWDG peripheral.

  4. Uses HAL_WWDG_Refresh() to manage the refresh of the WWDG counter.

  5. if wwdg interrupt raises, driver calls User_Callback_function .

Summary

  • The init function instantiates a handle and associates it with the WWDG instance.

  • The start function allows to start the WWDG according to the users parameters:
    • the prescaler is based on maximum time given by the user in milliseconds ( it can calso be customized to use microseconds or seconds) .

    • the reload is based on of maximum time given by the user in milliseconds ( it can calso be customized to use microseconds or seconds) .

    • the window is based on of minimum time given by the user in milliseconds ( it can calso be customized to use microseconds or seconds) .

 @startuml

 rnote over "User"
 <color #green> As a first step, the user manually calculates the values
 <color #green> of  reload, window, and prescaler and set them in the handle .
 <color #green> These values can also calculated by STM32CubeMX.
 endrnote


 "User"->"Driver WWDG" : HAL_WWDG_Init()


 "User" -> "Driver WWDG" : HAL_WWDG_RegisterCallback()


 alt WWDG not expired
 group Loop [Can be repeated]


 "User" -> "Driver WWDG" : HAL_WWDG_Refresh()

 rnote over "Driver WWDG"
 <color #green>Refresh done when the down-counter
 <color #green>value is inside the window
 endrnote

 rnote over "Driver WWDG"
 <color #green> Operation ended successfully
 endrnote

 end

 else WWDG expired
 rnote over "Driver WWDG"
 <color #red>When the down-counter is equal to 0x3F
 endrnote
"NVIC" -> "WWDG_IRQHandler" : WWDG Early Wakeup interrupt
"WWDG_IRQHandler" -> "Driver WWDG" : HAL_WWDG_IRQHandler()

 "Driver WWDG" -> "User" :HAL_WWDG_EarlyWakeupCallback()

 end

 @enduml

Code snippet

HAL1

HAL2

/* Counter and Window register values are already calculated by the user */
#define WWDG_WINDOW    0x50
#define WWDG_COUNTER   0x7F








/* WWDG handle */
WWDG_HandleTypeDef hwwdg;

/* Instance selection */
hwwdg.Instance = WWDG;

/* Set Init */
hwwdg.Init.Prescaler = WWDG_PRESCALER_64;
hwwdg.Init.Window    = WWDG_WINDOW;
hwwdg.Init.Counter   = WWDG_COUNTER;
hwwdg.Init.EWIMode   = WWDG_EWI_DISABLE;

/* Init the WWDG */
HAL_WWDG_Init(&hwwdg)

/* Registering the user callback function */
 HAL_WWDG_RegisterCallback(&hwwdg, HAL_WWDG_EWI_CB_ID, User_Callback_function);




/* WWDG refresh : need to be called periodically */
HAL_WWDG_Refresh(&hwwdg);

/* IRQHandler function */
 HAL_WWDG_IRQHandler(&hwwdg);

/* WWDG  callback function */
HAL_WWDG_EarlyWakeupCallback(&hwwdg, HAL_WWDG_EWI_CB_ID, User_Callback_function);
/* Prescaler, reload and window parameters are calculated based on max_time_ms and min_time_ms */

/* max_time_ms */
 uint32_t max_time_ms = 780;
/* min_time_ms */
 uint32_t min_time_ms = 400;
/* early wakeup status */
 uint32_t ewi_status = 0;



/* WWDG handle */
hal_wwdg_handle_t hwwdg;










/* Init the WWDG */
HAL_WWDG_Init(&hwwdg, HAL_WWDG1);

/* Registering the user callback function */
HAL_WWDG_RegisterEarlyWakeupCallback(&hwwdg, User_Callback_function);

/* Config and Start the WWDG: Counter, Prescaler are calculated from the max and min times */
HAL_WWDG_Start(&hwwdg, max_time_ms, min_time_ms, ewi_status);

/* WWDG refresh : need to be called periodically */
HAL_WWDG_Refresh(&hwwdg);

/* IRQHandler function */
 HAL_WWDG_IRQHandler(&hwwdg);

/* WWDG EWI  callback */
HAL_WWDG_EarlyWakeupCallback(&hwwdg);

HAL WWDG max time adjustment

The table below outlines the typical scenarios for HAL1 and HAL2 regarding the WWDG peripheral:

In this scenario user can adjust and modify the maximum time value, according to the current maximum time and the possible value of step.

  • Maximum time corresponds to “Reset time” given by the user in milliseconds ( it can calso be customized to use microseconds or seconds) .

HAL1 Sequence Diagram

HAL2 Sequence Diagram

 @startuml

rnote over "User"
As a first step, the user manually calculates the values
of  reload, window, and prescaler and set them in the handle .
These values can also calculated by STM32CubeMX.
endrnote

 "User"->"Driver WWDG" : HAL_WWDG_Init()
 group Loop [Can be repeated]
 "User" -> "Driver WWDG" : HAL_WWDG_Refresh()
 end

 rnote over "User"
  To change the Reload value,
  the user must recalculate it manually
  and set it in the handle .
 endrnote

 "User"->"Driver WWDG" : HAL_WWDG_Init()
 group Loop [Can be repeated]
 "User" -> "Driver WWDG" : HAL_WWDG_Refresh()

 end

 @enduml

@startuml

"User"->"Driver WWDG" : HAL_WWDG_Init()
"Driver WWDG" --> "User": hal status : HAL_OK

rnote over "User"
For PCLK = 160 MHz and time unit is microseconds
user chooses 77000 us as max time.
endrnote

"User" -> "Driver WWDG" : HAL_WWDG_Start()
"Driver WWDG" --> "User": hal status : HAL_OK

group Loop [Can be repeated]

"User" -> "Driver WWDG" : HAL_WWDG_Refresh()
end

rnote over "User"
To know the max time calculated by the WWDG driver(the exact value of the application)
user can call HAL_WWDG_GetMaxTime() function.
endrnote

"User"->"Driver WWDG" : HAL_WWDG_GetMaxTime()
"Driver WWDG" -> "User" : 75000 us

rnote over "User"
To know the wwdg step
user can call HAL_WWDG_GetStep_us() function.
endrnote

"User"->"Driver WWDG" : HAL_WWDG_GetStep_us()
"Driver WWDG" -> "User" : 1638 us

rnote over "User"
User can adjust the max time according
the current  time + step.
endrnote

"User" -> "Driver WWDG" : HAL_WWDG_Start()
"Driver WWDG" --> "User"

group Loop [Can be repeated]

"User" -> "Driver WWDG" : HAL_WWDG_Refresh()

end

@enduml

Code snippet

HAL1

HAL2

/* Counter and Window register values are already calculated by the user */

 #define WWDG_WINDOW    0x50
 #define WWDG_COUNTER   0x7F










 /* WWDG handle */
 WWDG_HandleTypeDef hwwdg;

 /* Instance selection */
 hwwdg.Instance = WWDG;

 /* Set Init */
 hwwdg.Init.Prescaler = WWDG_PRESCALER_64;
 hwwdg.Init.Window    = WWDG_WINDOW;
 hwwdg.Init.Counter   = WWDG_COUNTER;
 hwwdg.Init.EWIMode   = WWDG_EWI_DISABLE;

 /* Init the WWDG */
 HAL_WWDG_Init(&hwwdg);





 /* WWDG refresh : need to be called periodically */
 HAL_WWDG_Refresh(&hwwdg);

 /* User recalculate the value of counter manually and set it again */
 hwwdg.Init.Counter = 0x6F ;




 /* Init the WWDG */
 HAL_WWDG_Init(&hwwdg);

 /* WWDG refresh : need to be called periodically */
  HAL_WWDG_Refresh(&hwwdg);
/* Prescaler, reload and window parameters are calculated based on max_time_us and min_time_us */

/* max_time_us */
uint32_t max_time_us = 77000;
/* min_time_us */
uint32_t min_time_us = 40000;
/* early wakeup status */
uint32_t ewi_status = 0;
/* returned_max_time_us  */
uint32_t returned_max_time_us = 0;
/* returned_step_us  */
uint32_t returned_step_us = 0;


/* WWDG handle */
hal_wwdg_handle_t hwwdg;










/* Init the WWDG */
HAL_WWDG_Init(&hwwdg, HAL_WWDG1);

/* Start the WWDG : prescaler / reload / window and early wakeup are
 calculated from the max, min and early wakeup time */
HAL_WWDG_Start(&hwwdg, min_time_us, max_time_us, ewi_status);

/* WWDG refresh : need to be called periodically */
HAL_WWDG_Refresh(&hwwdg);

/* Get max time configured by the WWDG */
returned_max_time_us = HAL_WWDG_GetMaxTime(&hwwdg);

/* Get the WWDG step */
returned_step_us = HAL_WWDG_GetStep_us(&hwwdg);

/* adjust the maximum time according the current time and step  */
 HAL_WWDG_Start(&hwwdg, min_time_us,( returned_max_time_us + returned_step_us ), ewi_status);

/* WWDG refresh : need to be called periodically */
HAL_WWDG_Refresh(&hwwdg);

HAL WWDG min time adjustment

The table below outlines the typical scenarios for HAL1 and HAL2 regarding the WWDG peripheral:

In this scenario user modify the minimum time value,

  • Minimum time corresponds to “Window time” given by the user in milliseconds ( it can calso be customized to use microseconds or seconds) .

HAL1 Sequence Diagram

HAL2 Sequence Diagram

@startuml

rnote over "User"
As a first step, the user manually calculates the values
of  reload, window, and prescaler and set them in the handle .
endrnote
"User"->"Driver WWDG" : HAL_WWDG_Init()


group Loop [Can be repeated]

"User" -> "Driver WWDG" : HAL_WWDG_Refresh()
end
rnote over "User"
To change the window value, the user must recalculate it manually.
and set it in the handle .
endrnote

"User"->"Driver WWDG" : HAL_WWDG_Init()

group Loop [Can be repeated]

"User" -> "Driver WWDG" : HAL_WWDG_Refresh()
end

@enduml

@startuml


"User"->"Driver WWDG" : HAL_WWDG_Init()
"Driver WWDG" --> "User": hal status : HAL_OK

rnote over "User"
For PCLK = 160 MHz and time unit is milliseconds
user chooses 77 ms as min time .
endrnote

"User" -> "Driver WWDG" : HAL_WWDG_Start()
"Driver WWDG" --> "User": hal status : HAL_OK

group Loop [Can be repeated]


"User" -> "Driver WWDG" : HAL_WWDG_Refresh()
"Driver WWDG" --> "User": hal status : HAL_OK
end

rnote over "User"
To modify the min time assured by the WWDG driver
user can call HAL_WWDG_SetMinTime() function .
endrnote

"User"->"Driver WWDG" : HAL_WWDG_SetMinTime(): 30ms



group Loop [Can be repeated]

"User" -> "Driver WWDG" : HAL_WWDG_Refresh()
end

@enduml

Code snippet

HAL1

HAL2

/* Counter and Window register values are already calculated by the user */

#define WWDG_WINDOW    0x50
#define WWDG_COUNTER   0x7F







/* WWDG handle */
WWDG_HandleTypeDef hwwdg;

/* Instance selection */
hwwdg.Instance = WWDG;

/* Set Init */
hwwdg.Init.Prescaler = WWDG_PRESCALER_64;
hwwdg.Init.Window    = WWDG_WINDOW;
hwwdg.Init.Counter   = WWDG_COUNTER;
hwwdg.Init.EWIMode   = WWDG_EWI_DISABLE;

/* Init the WWDG */
HAL_WWDG_Init(&hwwdg);





/* WWDG refresh */
HAL_WWDG_Refresh(&hwwdg);


/* User recalculate the value of window manually and set it again */
hwwdg.Init.Window = 0x66 ;
/* Init the WWDG */
HAL_WWDG_Init(&hwwdg);

/* WWDG refresh */
 HAL_WWDG_Refresh(&hwwdg);
/* Prescaler, reload and window parameters are calculated based on max_time_ms and min_time_ms */

/* max_time_ms */
uint32_t max_time_ms = 77;
/* min_time_ms_1 */
uint32_t min_time_ms_1 = 40;
/* min_time_ms_2 */
uint32_t min_time_ms_2 = 30;
/* early wakeup status */
uint32_t ewi_status = 0;

/* WWDG handle */
hal_wwdg_handle_t hwwdg;










/* Init the WWDG */
HAL_WWDG_Init(&hwwdg, HAL_WWDG1);

/* Start the WWDG : prescaler / reload / window and early wakeup are
 calculated from the max, min and early wakeup time */
HAL_WWDG_Start(&hwwdg, min_time_ms, max_time_ms, ewi_status);

/* WWDG refresh : need to be called periodically */
HAL_WWDG_Refresh(&hwwdg);


/* Set the knew min_time_ms_2*/
HAL_WWDG_SetMinTime(&hwwdg , min_time_ms_2);


/* WWDG refresh : need to be called periodically */

HAL_WWDG_Refresh(&hwwdg);